//
//  SCNHomeTableViewCell.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/7.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit
import SnapKit

class SCNHomeTableViewCell: UITableViewCell {

    public var model: SCNHomeMarkerModels? {
        
        didSet{
         
            setData()
        }
        
    }

    

    /// 延时加载 lazy
    lazy var contentImageView: UIImageView = {
    
        let imgView = UIImageView(image:UIImage(named: "home_BG"))
        
        return imgView
    }()
    
    lazy var titleL: UILabel = {
       
        let lab = UILabel()
        lab.textColor = SCNConstants.colorFromHexValue(0x3A3A3A, alpha: 1)
        lab.text = "新手专享标"
        lab.font = UIFont.systemFont(ofSize: 20.0)
        lab.textAlignment = .center
        return lab
    }()
    
    lazy var tagImg: UIImageView = {
       
        let img = UIImageView(image: UIImage(named: "icon_huo"))
        
        return img
    }()
    
    lazy var typeImg: UIImageView = {
        
        let img = UIImageView(image: UIImage(named: "icon_xinshou_sanjiao"))
        
        return img
    }()

    lazy var pointL: UILabel = {
        
        let lab = UILabel()
        lab.backgroundColor = SCNConstants.colorFromHexValue(0xefefef, alpha: 1)
        return lab
    }()
    
    lazy var deadlineL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorFromHexValue(0x898989, alpha: 1)
        lab.text = "期限10天"
        lab.font = UIFont.systemFont(ofSize: 15.0)
        lab.textAlignment = .center
        return lab
    }()
    
    lazy var startMoneyL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorFromHexValue(0x898989, alpha: 1)
        lab.text = "起投100元"
        lab.font = UIFont.systemFont(ofSize: 15.0)
        lab.textAlignment = .center
        return lab
    }()
    
    lazy var mayMoneyL: UILabel = {
        
        let lab = UILabel()
        lab.textColor = SCNConstants.colorFromHexValue(0x898989, alpha: 1)
        lab.text = "可投100000元"
        lab.font = UIFont.systemFont(ofSize: 15.0)
        lab.textAlignment = .center
        return lab
    }()

    lazy var lineView1: UIView = {
        
        let view = UIView()
        view.backgroundColor = SCNConstants.colorFromHexValue(0xefefef, alpha: 1)
        return view
    }()
    lazy var lineView2: UIView = {
        
        let view = UIView()
        view.backgroundColor = SCNConstants.colorFromHexValue(0xefefef, alpha: 1)
        return view
    }()
    
    lazy var waveIndicator: WaveLoadingIndicator! = {
       
        let wave = WaveLoadingIndicator()
        wave.progress = 0.0
        
        return wave
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = SCNConstants.colorWith255RGBA(230, g: 230, b: 230, a: 1)
        
        contentView.addSubview(contentImageView)
        contentImageView.addSubview(titleL)
        contentImageView.addSubview(pointL)
        contentImageView.addSubview(tagImg)
        contentImageView.addSubview(typeImg)
        contentImageView.addSubview(deadlineL)
        contentImageView.addSubview(startMoneyL)
        contentImageView.addSubview(mayMoneyL)
        contentImageView.addSubview(lineView1)
        contentImageView.addSubview(lineView2)
        contentImageView.addSubview(waveIndicator)

        titleL.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 115, height: 20))
            make.centerX.equalTo(contentImageView)
            make.top.equalTo(12)
        }
//        makeConstraints()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let deadlineWidth = (self.frame.size.width - 20) / 3
        
        contentImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(5, 10, 5, 10))
            
        }

        pointL.snp.makeConstraints { (make) in
            make.leading.equalTo(10)
            make.top.equalTo(50)
            make.height.equalTo(1)
            make.trailing.equalTo(-10)
        }
        tagImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 26, height: 26))
            make.left.equalTo(titleL.snp.right)
            make.top.equalTo(5)
        }
        typeImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53, height: 42))
            make.trailing.equalTo(contentImageView)
            make.top.equalTo(contentImageView)
        }
        deadlineL.snp.remakeConstraints { (make) in
            make.leading.equalTo(0)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
        }
        startMoneyL.snp.remakeConstraints { (make) in
            make.leading.equalTo(deadlineL.snp.trailing)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
        }
        mayMoneyL.snp.remakeConstraints { (make) in
            make.trailing.equalTo(contentImageView)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
        }
//        deadlineL.snp.makeConstraints { (make) in
//            make.leading.equalTo(0)
//            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
////            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
//            make.height.equalTo(20)
//            make.width.equalTo(deadlineWidth)
//        }
//        startMoneyL.snp.makeConstraints { (make) in
//            make.leading.equalTo(deadlineWidth)
//            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
//            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
//        }
//        mayMoneyL.snp.makeConstraints { (make) in
//            make.trailing.equalTo(contentImageView)
//            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
//            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
//        }
        
        lineView1.snp.remakeConstraints { (make) in
            make.leading.equalTo(deadlineL.snp.trailing)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: 1, height: 20))
        }
        lineView2.snp.remakeConstraints { (make) in
            make.leading.equalTo(mayMoneyL.snp.leading)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: 1, height: 20))
        }
        let waveWidth = SCNConstants.SCREENHEIGHT >= 736 ? 190 : 155
        
        waveIndicator.snp.makeConstraints { (make) in
            make.center.equalTo(contentImageView)
            make.size.equalTo(CGSize(width: waveWidth, height: waveWidth))
        }
        

    }
    func setData() {
        
        titleL.text = model?.title
        
        let titleSzie = titleL.text?.getTextRectSize(font: UIFont.systemFont(ofSize: 22.0),size: CGSize(width: 300, height: CGFloat(MAXFLOAT)))
        if let size = titleSzie {
            titleL.snp.updateConstraints { (make) in
                make.size.equalTo(CGSize(width: size.width, height: 20))
            }
            
        }
        if let loandue = model?.loandue {
            deadlineL.text = "期限\(loandue)"
            deadlineL.attributedText = String().changeAttributedString(deadlineL.text!, range: NSRange(location: 2, length: (deadlineL.text?.characters.count)! - 2), font: 15, color: SCNConstants.rootBackgroundColor)
        }
        if let mintendermoney = model?.mintendermoney {
            let tmp = (mintendermoney as NSString).integerValue
            startMoneyL.text = "起投\(tmp)元"
            startMoneyL.attributedText = String().changeAttributedString(startMoneyL.text!, range: NSRange(location: 2, length: (startMoneyL.text?.characters.count)! - 2), font: 15, color: SCNConstants.rootBackgroundColor)
        }
        if let loanMoney = model?.loanmoney, let tender = model?.tendermoney {
            let tmp = (loanMoney as NSString).integerValue - (tender as NSString).integerValue
            mayMoneyL.text = "可投\(tmp)元"
            mayMoneyL.attributedText = String().changeAttributedString(mayMoneyL.text!, range: NSRange(location: 2, length: (mayMoneyL.text?.characters.count)! - 2), font: 15, color: SCNConstants.rootBackgroundColor)
        }
        if let progress = model?.advance {
            waveIndicator.progress = Double(progress) * 0.01
        }
        
        typeImg.image = model?.tagID == 4 ? UIImage(named: "icon_xinshou_sanjiao") : UIImage(named: "icon_qita_sanjiao")
    }
    
    func makeConstraints() {
        
        
        
        let deadlineWidth = (SCNConstants.SCREENWIDTH - 20) / 3
        
        
        contentImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(5, 10, 5, 10))
            
        }
        titleL.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 115, height: 20))
            make.centerX.equalTo(contentImageView)
            make.top.equalTo(12)
        }
        pointL.snp.makeConstraints { (make) in
            make.leading.equalTo(10)
            make.top.equalTo(50)
            make.height.equalTo(1)
            make.trailing.equalTo(-10)
        }
        tagImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 26, height: 26))
            make.left.equalTo(titleL.snp.right)
            make.top.equalTo(5)
        }
        typeImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53, height: 42))
            make.trailing.equalTo(contentImageView)
            make.top.equalTo(contentImageView)
        }
        deadlineL.snp.makeConstraints { (make) in
            make.leading.equalTo(0)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
        }
        startMoneyL.snp.makeConstraints { (make) in
            make.leading.equalTo(deadlineWidth)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
        }
        mayMoneyL.snp.makeConstraints { (make) in
            make.trailing.equalTo(contentImageView)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: deadlineWidth, height: 20))
        }
        lineView1.snp.makeConstraints { (make) in
            make.leading.equalTo(deadlineWidth)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: 1, height: 20))
        }
        lineView2.snp.makeConstraints { (make) in
            make.leading.equalTo(mayMoneyL.snp.leading)
            make.bottom.equalTo(contentImageView.snp.bottom).offset(-15)
            make.size.equalTo(CGSize(width: 1, height: 20))
        }
        waveIndicator.snp.makeConstraints { (make) in
            make.center.equalTo(contentImageView)
            make.size.equalTo(CGSize(width: 170, height: 170))
        }

        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
